require "RadioMenu"

RenWuDailyScene={}
RenWuDailySceneFunc={}
RenWuDailySceneFunc.__index = RenWuDailySceneFunc
setmetatable(RenWuDailyScene, RenWuDailySceneFunc)
RenWuDailyScene.NodeID = {QuestTable=1001, CloseButton=1002, NameLabel=1011, DescLabel=1012, StateLabel=1013, GainExpLabel=1014, GainCashLabel=1015}

RenWuDailySceneFunc.getNode = function()
  local l = {}
  setmetatable(l, RenWuDailySceneFunc)
  l.node = CCBReader:load("RenWuDailyScene.ccbi")
  print("RenWuDailyScene.getNode", RenWuDailyScene.node)
  l:init() 
  return l 
end

RenWuDailySceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(RenWuDailyScene:getNode().node)  
  print("RenWuDailyScene.getScene", sc)
  return sc
end

RenWuDailySceneFunc.init = function(self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1021), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  
  self:setTable()  
end

RenWuDailySceneFunc.setTable = function(self)
  if self.questTable~=nil then
    self.node:removeChild(self.questTable, true)
  end
  self.questTable = self:createRenWuTable() 
  self.node:addChild(self.questTable)
  if self.oldTableOffset~=nil then
    TypeCast(self.questTable, "CCScrollView"):setContentOffset(self.oldTableOffset)  
  end
  if self.currentIndex==nil then
    self.currentIndex = self.items:count()
  end
  --self.currentRenWuID = self.dailyQuests[self.currentIndex].id
  --self.focusItem = TypeCast(self.items:objectAtIndex(index), "CCLayer")
  self:setFocus() 
  self:setRenWuLabel()

  local t = UserData.userinfo.host.triggerHash
  
  local days = {[1]=0, [2]=0, [3]=0, [4]=0, [5]=0, [6]=0, [7]=0}
  if t.friday~=nil then days[1]=t.friday;end
  if t.saturday~=nil then days[2]=t.saturday;end
  if t.sunday~=nil then days[3]=t.sunday;end
  if t.monday~=nil then days[4]=t.monday;end
  if t.tuesday~=nil then days[5]=t.tuesday;end
  if t.wednesday~=nil then days[6]=t.wednesday;end
  if t.thursday~=nil then days[7]=t.thursday;end

  local scores = string.format("%3d%7d%7d%7d%7d%7d%7d", days[1], days[2], days[3], days[4], days[5], days[6], days[7])
  TypeCast(self.node:getChildByTag(1016), "CCLabelTTF"):setString(scores)
  local tt = days[1]+days[2]+days[3]+days[4]+days[5]+days[6]+days[7]
  TypeCast(self.node:getChildByTag(1017), "CCLabelTTF"):setString(tostring(tt))
  --local lb = CCLabelTTF:create(scores, "Helvetica", 28)
  --local bk = self.node:getChildByTag(1030)
  --local x,y = bk:getPosition()
  --lb:setPosition(ccp(x-10, y))
  --self.node:addChild(lb)
  --self.scoreLabel = lb
end
--[[
RenWuSceneFunc.getQuestInfo = function(self, qid)
  for k,v in pairs(UserData.userinfo.quests) do
    if v.id==qid then return v;end
  end
end]]

RenWuDailySceneFunc.setFocus = function(self)
  for i=0,self.items:count()-1 do
    local tmp = TypeCast(self.items:objectAtIndex(i), "CCLayer")
    tmp:removeChildByTag(100002, true)
  end
  local item = self.items:objectAtIndex(self.currentIndex-1)
  local sp = CCSprite:create("dailyquest_focus.png")
  --local x,y= item:getChildByTag(100001):getPosition()
  --sp:setPosition(ccp(x,y))
  sp:setPosition(ccp(145,36))
  sp:setTag(100002)
  item:addChild(sp)
end

RenWuDailySceneFunc.createRenWuItem = function(self, qid, index)
  local quest = Quest:create(qid)--self:getQuestInfo(qid)--UserData.userinfo.quests[tostring(qid)]
  local l = CCLayer:create()
 
  print(quest.info.title) 
  local imgName="dailyquest_bk4.png" 
  local button = CCButton:createWithFileName("onSelectQuest", imgName, imgName, imgName, "")
  button:setPosition(ccp(145,36))
  setObjectExtTable(button, self)
  button:setTag(index)
  l:addChild(button)

  print("RenWuDailySceneFunc.createRenWuItem", qid) 
  local lb = CCLabelTTF:create(string.format("%sLv%d", quest.info.title, quest:getLevel()), "Helvetica", 28)
  lb:setPosition(ccp(115,36))
  --lb:setAnchorPoint(ccp(0.8, 0.5))
  lb:setTag(100001)
  l:addChild(lb)
  
  local curQuest = UserData.userinfo.host.currentQuests[tostring(qid)]
  if curQuest==nil then
    print("RenWuDailySceneFunc.createRenWuItem set flag", qid) 
    local sp = CCSprite:create("dailyquest_complete.png")
    sp:setPosition(ccp(250,36))
    l:addChild(sp)
  elseif curQuest==quest.info.threshold then
    print("RenWuDailySceneFunc.createRenWuItem set flag", qid) 
    local sp = CCSprite:create("renwu_done.png")
    sp:setPosition(ccp(250,36))
    l:addChild(sp)
  else
    print("RenWuDailySceneFunc.createRenWuItem set flag", qid) 
    local sp = CCSprite:create("dailyquest_lock0001.png")
    sp:setPosition(ccp(250,36))
    l:addChild(sp)
  end
  l:setContentSize(CCSizeMake(300, 75)) 
  return l
end

RenWuDailySceneFunc.createRenWuTable = function(self)
  --local quests = UserData.userinfo.quests
  local quests = UserData.userinfo.host.current.d
  local tmp = {}
  --排序
  for k,v in pairs(quests) do
    local q = self:getQuestInfo(tonumber(k))
    local c = UserData.userinfo.host.currentQuests[tostring(k)]
    if c==nil then
      tmp[#tmp+1] = tonumber(k)
    end 
  end
  for k,v in pairs(quests) do
    local q = self:getQuestInfo(tonumber(k))
    local c = UserData.userinfo.host.currentQuests[tostring(k)]
    if c~=nil and c~=q.threshold then
      tmp[#tmp+1] = tonumber(k)
    end 
  end
  for k,v in pairs(quests) do
    local q = self:getQuestInfo(tonumber(k))
    local c = UserData.userinfo.host.currentQuests[tostring(k)]
    if c~=nil and c==q.threshold then
      tmp[#tmp+1] = tonumber(k)
    end 
  end

  self.dailyQuests = {}
  local items = CCArray:create()
  for k,v in ipairs(tmp) do
    local item = self:createRenWuItem(tonumber(v), #self.dailyQuests+1) 
    items:addObject(item)
    self.dailyQuests[#self.dailyQuests+1] = self:getQuestInfo(tonumber(v))--UserData.userinfo.quests[v]
  end
  print("RenWuDailySceneFunc.initRenWuTable items", items:count())
  --local tableBK = self.node:getChildByTag(RenWuDailyScene.NodeID.QuestTable)
  --local size = tableBK:getContentSize()
  local tableview = CCTableView:create(1, CCSizeMake(300, 230), items)
  tableview:setPosition(60, 100)
  self.questTable = tableview
  self.items = items
  return tableview
end

RenWuDailySceneFunc.getQuestInfo = function(self, rid)
  for k,v in pairs(UserData.userinfo.quests) do
    --print("RenWuDailySceneFunc.getQuestInfo", rid, k, v.actionItem)
    if v.id==rid then
      return v
    end
  end
end

RenWuDailySceneFunc.onSelectQuest = function(self, node, event)
  local qid = self.dailyQuests[node:getTag()].id
  --local qid = node:getTag()
  --self.currentRenWuID = qid 
  self.currentIndex = node:getTag()
  print("RenWuDailySceneFunc.onSelectQuest", qid)
  --local item = TypeCast(self.items:objectAtIndex(0), "CCLayer")
  self:setFocus() 
  self:setRenWuLabel()
end

RenWuDailySceneFunc.setRenWuLabel = function(self)
  local questItems = Array:create()
  --local quest = UserData.userinfo.quests[tostring(self.currentRenWuDailyID)]
  local qid = self.dailyQuests[self.currentIndex].id
  local quest = Quest:create(qid)--self:getQuestInfo(qid) 
   
  local label = self.node:getChildByTag(RenWuDailyScene.NodeID.DescLabel)
  TypeCast(label, "CCLabelTTF"):setString(quest.info.desc)
  label = self.node:getChildByTag(RenWuDailyScene.NodeID.NameLabel)
  TypeCast(label, "CCLabelTTF"):setString(quest.info.title)
  label = self.node:getChildByTag(RenWuDailyScene.NodeID.GainExpLabel)
  TypeCast(label, "CCLabelTTF"):setString(tostring(quest.info.xpReward))
  label = self.node:getChildByTag(RenWuDailyScene.NodeID.GainCashLabel)
  TypeCast(label, "CCLabelTTF"):setString(tostring(quest.info.cashReward))
  label = self.node:getChildByTag(1018)
  TypeCast(label, "CCLabelTTF"):setString(tostring(quest:getPoint()))
  if self.stars~=nil then
    for k,v in pairs(self.stars) do
      v:getParent():removeChild(v, true)
    end
  end
  self.stars = {}
  for i=1,quest:getLevel() do
    local sp = addSpriteFromBK(self.node:getChildByTag(1002), i, "dailyquest_star0002.png")
    self.stars[#self.stars+1] = sp
  end  
 
  label = self.node:getChildByTag(RenWuDailyScene.NodeID.StateLabel)
  local sp = self.node:getChildByTag(1031)
  local button = self.node:getChildByTag(1020)
  local tmp = UserData.userinfo.host.currentQuests[tostring(qid)]
  if tmp~=nil then
    TypeCast(label, "CCLabelTTF"):setString(string.format("%d/%d", tmp, quest.info.threshold))
    sp:setVisible(false) 
    button:setVisible(true) 
  else
    TypeCast(label, "CCLabelTTF"):setString(string.format("%d/%d", quest.info.threshold, quest.info.threshold))
    sp:setVisible(true) 
    button:setVisible(false) 
  end
end

RenWuDailySceneFunc.onCloseRankWnd = function(self, node, event)
  self.node:removeChild(node:getParent(), true)
  setTouchEnabledAll(self.node, true)
end
RenWuDailySceneFunc.showRankWnd = function(self)
  setTouchEnabledAll(self.node, false)
  self.rankWnd = CCBReader:load("DailyQuestRankScene.ccbi") 
  local radioMenu = RadioMenu:create(self.rankWnd:getChildByTag(1001), "menuItemChanged", self)
  setObjectExtTable(self.rankWnd:getChildByTag(1111), self)
  self.currentRank = 0 
  radioMenu:setMenuState(self.currentRank) 
  self.node:addChild(self.rankWnd)
end
RenWuDailySceneFunc.createRankTable = function(self)
  local items = CCArray:create()
  local ranks = UserData.userinfo.models.IntegralTop20
  if self.currentRank==1 then
    ranks = UserData.userinfo.models.LastIntegralTop20
  end
  for i=1,#ranks do
    local v = ranks[#ranks-i+1]
    local item = CCBReader:load("DailyQuestRankItem.ccbi")
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(tostring(v.rating))
    TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(v.name)
    TypeCast(item:getChildByTag(1012), "CCLabelTTF"):setString(tostring(v.value))
    items:addObject(item)
    item:setContentSize(CCSizeMake(740,50))
  end
  
  local tableview = CCTableView:create(1, CCSizeMake(750, 320), items)
  tableview:setPosition(100, 115)
  if self.rankTable~=nil then
    self.rankWnd:removeChild(self.rankTable, true)
  end
  self.rankWnd:addChild(tableview)
  self.rankTable = tableview
end
RenWuDailySceneFunc.menuItemChanged = function(self, oldIndex, newIndex)
  self.currentRank = newIndex
  self:createRankTable()
end
RenWuDailySceneFunc.onRankButton = function(self, node, event)
  self:showRankWnd()
end
RenWuDailySceneFunc.onRefreshButton = function(self, node, event)
  print("RenWuDailySceneFunc.onRefreshButton")
end

RenWuDailySceneFunc.onCompleteRenWuButton = function(self, node, event)
  self.oldTableOffset = TypeCast(self.questTable, "CCScrollView"):getContentOffset()
  local qid = self.dailyQuests[self.currentIndex].id
  NetworkRequest.completeQuest(qid, "onCompleteRenWuComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
  --UserData.userinfo.host.currentQuests[tostring(qid)]=nil
end

RenWuDailySceneFunc.onCompleteRenWuComplete = function(self, data)
  print("RenWuDailySceneFunc.onCompleteRenWuDailyComplete", data)
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage(string.format("任务完成 xp:%d cash:%d", res.data.xpReward, res.data.cashReward), ccc3(0,255,0))
    local qid = self.dailyQuests[self.currentIndex].id
    UserData.userinfo.host.currentQuests[tostring(qid)]=nil
    self:setTable()
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end

RenWuDailySceneFunc.onClose = function(self,node)
  print("onCloseRenWuDailyScene")
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  RenWuDailyScene.instance = nil
end
